package com.lucifer.cloud.boot.blog.domin.dto.comment;

import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.lucifer.cloud.boot.blog.domin.bo.Comment;
import com.lucifer.cloud.boot.blog.domin.bo.User;
import com.lucifer.cloud.boot.blog.domin.dto.user.UserConverter;
import com.lucifer.cloud.boot.blog.domin.dto.user.UserInfo;
import com.lucifer.cloud.boot.blog.util.GenerateUtils;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author lucifer
 * @date 2024/1/13 10:01
 */
public class CommentConverter {

    public static CommentDto converter2Dto(List<Comment> commentList,Long count, List<User> userList){
        List<CommentInfo> infos = Optional.ofNullable(commentList).orElse(Lists.newArrayList())
                .stream()
                .map(comment -> {
                    CommentInfo commentInfo = new CommentInfo();
                    BeanUtils.copyProperties(comment, commentInfo);
                    commentInfo.setUid(String.valueOf(comment.getUid()));
                    String user_id = String.valueOf(comment.getUser_id());
                    Map<String, UserInfo> id2UserInfoMap = Optional.ofNullable(userList).orElse(Lists.newArrayList())
                            .stream().map(user -> {
                                return UserConverter.convertInfo(user);
                            }).collect(Collectors.toMap(UserInfo::getId, Function.identity()));
                    UserInfo userInfo = id2UserInfoMap.get(user_id);
                    commentInfo.setUser_info(userInfo);
                    commentInfo.setUser_id(user_id);
                    return commentInfo;
                }).collect(Collectors.toList());

        return CommentDto.builder().infos(infos).count(count).build();
    }


    public static Comment converterReq2Comment(String contentId, String content,String createdUserId, String type,Long userId){
        Comment comment = new Comment();
        comment = (Comment)GenerateUtils.generateFiledValue(comment);
        comment.setContent(content);
        comment.setType(type);
        comment.setUser_id(userId);
        comment.setCreated_user_id(Long.valueOf(createdUserId));
        comment.setContent_id(Long.valueOf(contentId));
        return comment;
    }
}
